package com.fr.data.impl;

import com.fr.base.core.ComparatorUtils;
import com.fr.base.xml.BaseXMLUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class JNDIDatabaseConnection extends AbstractDatabaseConnection
{
  private String jndiName;
  private Hashtable contextHashtable = new Hashtable();

  public String getJNDIName()
  {
    return this.jndiName;
  }

  public void setJNDIName(String paramString)
  {
    this.jndiName = paramString;
  }

  public Hashtable getContextHashtable()
  {
    return this.contextHashtable;
  }

  public void setContextHashtable(Hashtable paramHashtable)
  {
    this.contextHashtable = paramHashtable;
  }

  public void testConnection()
    throws Exception
  {
    Object localObject = new InitialContext(this.contextHashtable);
    if (this.contextHashtable.get("java.naming.factory.initial") == null)
      localObject = (Context)((Context)localObject).lookup("java:comp/env");
    DataSource localDataSource = (DataSource)((Context)localObject).lookup(this.jndiName);
    Connection localConnection = localDataSource.getConnection();
    localConnection.close();
  }

  public Connection createConnection()
    throws Exception
  {
    Object localObject = new InitialContext(this.contextHashtable);
    if (this.contextHashtable.get("java.naming.factory.initial") == null)
      localObject = (Context)((Context)localObject).lookup("java:comp/env");
    DataSource localDataSource = (DataSource)((Context)localObject).lookup(this.jndiName);
    return ((Connection)localDataSource.getConnection());
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof JNDIDatabaseConnection) && (super.equals(paramObject)) && (ComparatorUtils.equals(getJNDIName(), ((JNDIDatabaseConnection)paramObject).getJNDIName())));
  }

  public String toString()
  {
    return "[JNDIName:" + getJNDIName() + "]";
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if ((paramXMLableReader.isChildNode()) && ("JNDIDatabaseAttr".equals(paramXMLableReader.getTagName())))
    {
      String str;
      if ((str = paramXMLableReader.getAttr("jndiName")) != null)
        setJNDIName(str);
      paramXMLableReader.readXMLObject(new XMLReadable(this)
      {
        private final JNDIDatabaseConnection this$0;

        public void readXML()
        {
          if (paramXMLableReader.isAttr())
            JNDIDatabaseConnection.access$000(this.this$0).clear();
          if (paramXMLableReader.isChildNode())
          {
            String str;
            if ((str = paramXMLableReader.getElementValue()) != null)
              JNDIDatabaseConnection.access$000(this.this$0).put(paramXMLableReader.getTagName(), str);
          }
        }
      });
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("JNDIDatabaseAttr").attr("jndiName", getJNDIName());
    Enumeration localEnumeration = this.contextHashtable.keys();
    while (localEnumeration.hasMoreElements())
    {
      Object localObject = localEnumeration.nextElement();
      String str = BaseXMLUtils.xmlAttrEncode((String)localObject);
      paramXMLPrintWriter.startTAG(str).textNode((String)this.contextHashtable.get(localObject)).end();
    }
    paramXMLPrintWriter.end();
  }

  static Hashtable access$000(JNDIDatabaseConnection paramJNDIDatabaseConnection)
  {
    return paramJNDIDatabaseConnection.contextHashtable;
  }
}